﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vowei.Data;
using Vowei.Data.Models;
using Vowei.Core.Models;

namespace vowei.test.ModelTest
{
    [TestClass]
    public class ForeignKeyTest : TestBase
    {
        [TestMethod]
        public void RequirementMilestoneFkTest()
        {
            var project = CreateProjectHelper("RequirementMilestoneFkTest");
            var requirement = CreateRequirementHelper("RequirementMilestoneFkTest", project);

            using (var impl = new VoweiContextImpl())
            {
                impl.Requirements.Add(requirement);
                impl.SaveChanges();
            }

            using (var impl = new VoweiContextImpl())
            {
                var actual = impl.Requirements.Include("BelongsToMilestone").Single(r => r.ID == requirement.ID);
                Assert.IsNotNull(actual.BelongsToMilestone);
                Assert.AreEqual(project.ID, actual.BelongsToMilestone.ID);
            }
        }

        [TestMethod]
        public void RequirementChildFkTest()
        {
            var project = CreateProjectHelper("RequirementChildFkTest");
            var requirement = CreateRequirementHelper("RequirementChildFkTest", project);
            var task1 = CreateTaskHelper("RequirementChildFkTest1", project);
            var task2 = CreateTaskHelper("RequirementChildFkTest2", project);
            requirement.Children.Add(task1);

            using (var impl = new VoweiContextImpl())
            {
                impl.Requirements.Add(requirement);
                impl.SaveChanges();
            }

            using (var impl = new VoweiContextImpl())
            {
                var actual = impl.Requirements.Find(requirement.ID);
                Assert.AreEqual(1, actual.Children.Count());
                Assert.AreEqual(task1.ID, actual.Children[0].ID);

                impl.Milestones.Attach(task2.BelongsToMilestone);
                actual.Children.Add(task2);
                impl.SaveChanges();
            }

            using (var impl = new VoweiContextImpl())
            {
                var actual = impl.Requirements.Find(requirement.ID);
                Assert.AreEqual(2, actual.Children.Count());

                Assert.IsTrue(actual.Children.Any(t => t.ID == task1.ID));
                Assert.IsTrue(actual.Children.Any(t => t.ID == task2.ID));
            }
        }

        [TestMethod]
        public void ProjectTeamMemberForeignKeyTest()
        {
            var project = CreateProjectHelper("ProjectTeamMemberForeignKeyTest");
            var employees = new Employee[]
            {
                new Employee() { Email = "employee1@vowei.com" },
                new Employee() { Email = "employee2@vowei.com" }
            };

            using (var impl = new VoweiContextImpl())
            {
                var context = new VoweiContext(impl);

                foreach (Employee employee in employees)
                {
                    if (!context.Employees.Query.Any(e => e.Email == employee.Email))
                    {
                        context.Employees.Add(employee);
                    }
                    // project.ResourceAssignments.Add(new ResourceAssignment() { ProjectId = project.ID, User = employee.Email });
                    project.ResourceAssignments.Add(new ResourceAssignment() { ProjectId = project.ID, ResourceId = employee.ID });
                }

                context.Projects.Add(project);
                context.SaveChanges();
            }

            using (var impl = new VoweiContextImpl())
            {
                var context = new VoweiContext(impl);
                var actual = context.Projects.Include("ResourceAssignments").Query.Single(p => p.ID == project.ID);
                Assert.IsNotNull(actual.ResourceAssignments);
                Assert.AreEqual(2, actual.ResourceAssignments.Count());

                foreach (Employee employee in employees)
                {
                    var id = employee.ID;
                    Assert.IsTrue(actual.ResourceAssignments.Any(a => a.ResourceId == id));
                }
            }            
        }

        [TestMethod]
        public void TicketChildrenParentIdTest()
        {
            var project = CreateProjectHelper("TicketChildrenParentIdTest");
            var requirement = CreateRequirementHelper("TicketChildrenParentIdTest", project);
            var task = CreateTaskHelper("TicketChildrenParentIdTest", project);

            requirement.Children.Add(task);
            using (var impl = new VoweiContextImpl())
            {
                impl.Requirements.Add(requirement);
                impl.SaveChanges();
            }

            using (var impl = new VoweiContextImpl())
            {
                var actual = impl.Tasks.Find(task.ID);
                var aq = impl.Requirements.Include("Children").First(r => r.ID == requirement.ID);
                Assert.IsNotNull(actual.ParentId);
                Assert.AreEqual(requirement.ID, actual.ParentId);
                Assert.AreEqual(1, aq.Children.Count);
                Assert.AreEqual(task.ID, aq.Children[0].ID);
            }
        }
    }
}
